package qx.hyj.petrescue.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qx.hyj.petrescue.Controller.TestController;
import qx.hyj.petrescue.VO.BlogVo;
import qx.hyj.petrescue.VO.CommentVo;
import qx.hyj.petrescue.constens.ErrorCode;
import qx.hyj.petrescue.dto.BlogType;
import qx.hyj.petrescue.entity.Blog;
import qx.hyj.petrescue.entity.Comment;
import qx.hyj.petrescue.entity.Users;
import qx.hyj.petrescue.mapper.Usermapper;
import qx.hyj.petrescue.service.BlogService;
import qx.hyj.petrescue.mapper.Blogmapper;

import java.util.Collections;
import java.util.List;

@Service
public class BlogServiceImpl implements BlogService {

    private final static Logger logger = LoggerFactory.getLogger(BlogServiceImpl.class);

    @Autowired
    private Usermapper usermapper;
    @Autowired
    private Blogmapper Blogmapper;

    @Override
    public List<BlogVo> Blogs(BlogType blogType1) {
        if (blogType1 == null) {
            throw new IllegalArgumentException("blogType1 cannot be null");
        }

        Integer dwType = blogType1.getBlogType();
        Integer blogType = blogType1.getBlogType();
        String search = blogType1.getSearch();

        if (dwType == null && blogType == null && search == null) {
            List<BlogVo> blogList = Blogmapper.getOldestBlogs(4);
            return setAuthorInfo(blogList);
        } else if (search == null) {
            List<BlogVo> blogList = Blogmapper.getTypeBlogs(blogType, dwType);
            return setAuthorInfo(blogList);
        } else {
            if (search != null && search.startsWith("@")) {
                search = search.substring(1);
                Users user = usermapper.login(search);
                if (user == null) {
                    throw new IllegalArgumentException("User not found for search: " + search);
                }
                Integer authorId = user.getId();
                List<BlogVo> blogList = Blogmapper.searchBlogsByAuthor(authorId);
                return setAuthorInfo(blogList);
            } else {
                List<BlogVo> blogList = Blogmapper.searchBlogsBysearch(search);
                return setAuthorInfo(blogList);
            }
        }
    }

    @Override
    public Integer addBlog(Blog blog) {
       Integer result = Blogmapper.addBlog(blog);
       return result;
    }

    @Override
    public Integer addComment(Comment comment) {
        Integer result = Blogmapper.addComment(comment);
        return result;
    }

    @Override
    public List<CommentVo> getComment(Integer blogId) {
        try {
            List<CommentVo> comments = Blogmapper.getComments(blogId);
            comments.forEach(commentVo -> {
                Users author = usermapper.searchById(commentVo.getCuser());
                commentVo.setCuserName(author.getNickname());
                commentVo.setCuserAvatar(author.getAvatar());
            });
            return comments;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer read(Integer blogId) {
        Blog blog1 = new Blog();
        try {
            Blogmapper.updateRead(blogId);
            blog1.setId(blogId);
            Blog blog = Blogmapper.searchByColumn(blog1);
            Integer readed = blog.getReaded();
            return readed;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public List<BlogVo> setAuthorInfo(List<BlogVo> blogList) {
        try {
            if (blogList == null) {
                return Collections.emptyList();
            }
            blogList.forEach(blogVo -> {
                Users author = usermapper.searchById(blogVo.getAuthor());
                if (author != null) {
                    blogVo.setAuthorName(author.getNickname());
                    blogVo.setAuthorPhone(author.getPhone());

                }
            });
            return blogList;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("setAuthorInfo failed", e);
            throw e;
        }
    }
}